//
//  GQProfileSearbarController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/29.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQProfileSearbarController.h"
#import "GQHomeSearchBar.h"

@interface GQProfileSearbarController () <UITextFieldDelegate>

@property (weak, nonatomic) GQHomeSearchBar *searchBar;
@property (strong, nonatomic) UIView *backgroundView;

@end

@implementation GQProfileSearbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 15)];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    CGFloat searchWidth = self.view.width - 73;
    CGFloat searchHeight = 25;
    GQHomeSearchBar *searchBar = [[GQHomeSearchBar alloc] init];
    self.searchBar = searchBar;
    searchBar.x = 15;
    searchBar.y = self.navigationController.navigationBar.height - 10 - searchHeight;
    searchBar.width = searchWidth;
    searchBar.height = searchHeight;
    searchBar.placeholder = @"手机号/姓名";
    searchBar.delegate = self;
    searchBar.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.navigationController.navigationBar addSubview:searchBar];
    [self setUpBackgroundView];
    
    [self.searchBar becomeFirstResponder];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:@"UITextFieldTextDidChangeNotification" object:self.searchBar];
    
}

//- (void)textFieldTextDidChange:(NSNotification *)noti
//{
//    UITextField *textField = (UITextField *)noti.object;
//    NSString *text = textField.text;
//    
//    NSError *error = NULL;
//    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[A-Za-z0-9\u4E00-\u9FA5_-]+$" options:NSRegularExpressionCaseInsensitive error:&error];
//    NSTextCheckingResult *result = [regex firstMatchInString:text options:0 range:NSMakeRange(0, text.length)];
//    if (result == nil) {
//        UITextRange *selectedRange = [textField markedTextRange];
//        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
//        NSLog(@"%ld", text.length);
//        NSLog(@"%@", position);
//        if (position) {
//            
//        }
//    }else{
//        textField.text = [text substringToIndex:text.length];
//    }
//    
//}

- (void)setUpBackgroundView
{
    UIView *bkView = [[UIView alloc] initWithFrame:self.view.bounds];
    bkView.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1.0];
    [self.view addSubview:bkView];
    
    UILabel *label = [[UILabel alloc] init];
    label.x = 15;
    label.y = kNavigatationBarHeight + 13;
    label.width = 120;
    label.height = 20;
    label.text = @"搜索古趣好友";
    label.textColor = [UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0];
    label.font = [UIFont systemFontOfSize:17];
    [bkView addSubview:label];
}

- (void)backToPre
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.searchBar removeFromSuperview];
    
}

//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:self.searchBar];
//}


@end
